package priv.yiyuan.example.design;

import java.util.ArrayList;
import java.util.List;

/**
 * 外观类
 *
 * @author YiYuan
 * @data 2025/8/8
 * @apoNote
 */
public class FileManagerFacade {

    /**
     * 文件系统引用(适配器引用)
     */
    private FileSystem currentFileSystem;

    private List<String> operationLog = new ArrayList<>();

    public FileManagerFacade(String osType) {
        switch (osType.toLowerCase()) {
            case "windows":
                currentFileSystem = new WindowsFileSystemAdapter(new WindowsNativeFileSystem());
                break;
            case "linux":
                currentFileSystem = new LinuxFileSystemAdapter(new LinuxNativeFileOperations());
                break;
            default:
                throw new IllegalArgumentException("Unsupported OS type: " + osType);
        }
    }

    public void switchFileSystem(String osType) {
        switch (osType.toLowerCase()) {
            case "windows":
                currentFileSystem = new WindowsFileSystemAdapter(new WindowsNativeFileSystem());
                break;
            case "linux":
                currentFileSystem = new LinuxFileSystemAdapter(new LinuxNativeFileOperations());
                break;
            default:
                throw new IllegalArgumentException("Unsupported OS type: " + osType);
        }

        this.logOperation("Switched to " + osType + " file system");
    }

    /**
     * 创建并写入文件
     *
     * @param path
     * @param content
     */
    public void createAndWriteFile(String path, String content) {
        currentFileSystem.createFile(path);
        currentFileSystem.writeFile(path, content);
        this.logOperation("Created and wrote to file: " + path);
    }

    /**
     * 读取并删除文件
     *
     * @param path
     * @return
     */
    public String readAndDeleteFile(String path) {
        String content = currentFileSystem.readFile(path);
        currentFileSystem.deleteFile(path);
        this.logOperation("Read and deleted file: " + path);
        return content;
    }

    /**
     * 获取文件信息摘要
     *
     * @param path 文件地址
     * @return
     */
    public String getFileSummary(String path) {
        String info = currentFileSystem.getFileInfo(path);
        this.logOperation("Retrieved file info: " + path);
        return "File Summary: \n" + info + "\nContent Preview: " + currentFileSystem.readFile(path).substring(0, Math.min(20, info.length()));
    }

    public List<String> getOperationLog() {
        return new ArrayList<>(operationLog);
    }

    private void logOperation(String message) {
        operationLog.add(message);
    }
}
